/*
 * Copyright (c) 2008 Golden T Studios.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package mrusanov.sounds;


import java.net.URL;

public abstract class BaseAudioRenderer {

    public enum Status {
        PLAYING, STOPPED, END_OF_SOUND, ERROR
    }


	private AudioEventsListener audioEventsListener;

	private Status status;


	public BaseAudioRenderer() {
		setStatus(Status.STOPPED);
	}

	protected abstract void playSound(URL audiofile);

	public void play(URL audiofile) {
		setStatus(Status.PLAYING);
		playSound(audiofile);
    }

	public AudioEventsListener getAudioEventsListener() {
		return audioEventsListener;
	}

	public void setAudioEventsListener(AudioEventsListener audioEventsListener) {
		this.audioEventsListener = audioEventsListener;
	}

	public Status getStatus() {
		return status;
	}

	protected void setStatus(Status status) {
		if (this.status != status) {
			this.status = status;

			if (getAudioEventsListener() != null) {
				getAudioEventsListener().notifyStatusChange(status);
				if (status == Status.END_OF_SOUND || status == Status.ERROR) {
					setAudioEventsListener(null);
				}
			}
		}
	}
 }
